package com.example.wechat.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wechat.R;
import com.example.wechat.model.SettingItem;
import java.util.List;

public class SettingsAdapter extends RecyclerView.Adapter<SettingsAdapter.ViewHolder> {
    private List<SettingItem> settings;
    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public SettingsAdapter(List<SettingItem> settings) {
        this.settings = settings;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_setting, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SettingItem item = settings.get(position);
        
        if (item.isHeader()) {
            holder.itemView.setBackgroundResource(R.color.gray_light);
            holder.titleView.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.gray));
            holder.subtitleView.setVisibility(View.GONE);
            holder.iconView.setVisibility(View.GONE);
            holder.arrowView.setVisibility(View.GONE);
        } else {
            holder.itemView.setBackgroundResource(R.color.white);
            holder.titleView.setTextColor(holder.itemView.getContext().getResources().getColor(R.color.black));
            holder.subtitleView.setVisibility(View.VISIBLE);
            holder.iconView.setVisibility(View.VISIBLE);
            holder.arrowView.setVisibility(View.VISIBLE);
            
            if (item.getIconResId() != 0) {
                holder.iconView.setImageResource(item.getIconResId());
            }
        }
        
        holder.titleView.setText(item.getTitle());
        holder.subtitleView.setText(item.getSubtitle());
        
        holder.itemView.setOnClickListener(v -> {
            if (listener != null && !item.isHeader()) {
                listener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return settings.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView titleView;
        TextView subtitleView;
        ImageView iconView;
        ImageView arrowView;

        ViewHolder(View view) {
            super(view);
            titleView = view.findViewById(R.id.setting_title);
            subtitleView = view.findViewById(R.id.setting_subtitle);
            iconView = view.findViewById(R.id.setting_icon);
            arrowView = view.findViewById(R.id.setting_arrow);
        }
    }
} 